package com.movitech.mobile.cz.base.common.util;

import com.aspose.cells.Workbook;
import com.aspose.words.*;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

public class Doc2Pdf {

    public static void main(String[] args) throws Exception {
//        doc2pdf("/Users/zwy/Desktop/接口文档补充.docx", "/Users/zwy/Desktop/test.pdf");
        excel2pdf("C:\\Users\\Administrator\\Desktop\\记录问题20180913(2).xlsx", "C:\\Users\\Administrator\\Desktop\\test.pdf");
    }

    public static boolean getLicense() throws Exception {
        boolean result = false;
        try {

            InputStream is = Document.class
                    .getResourceAsStream("/com.aspose.words.lic_2999.xml");
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        return result;
    }

    public static void doc2pdf(String inPath, String outPath) throws Exception {
        if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档有水印
            throw new Exception("com.aspose.words lic ERROR!");
        }
        System.out.println(inPath + " -> " + outPath);

        try {
            long old = System.currentTimeMillis();
            File file = new File(outPath);
            FileOutputStream os = new FileOutputStream(file);

            FontSettings f = new FontSettings();
            //f.setFontsFolder("/application/server/fonts", false);
            LoadOptions loadOptions = new LoadOptions();
            f.setFontsFolder("/application/server/fonts", false);
            loadOptions.setFontSettings(f);
            Document doc = new Document(inPath,loadOptions);

            //Document doc = new Document(inPath); // word文档
            // 支持RTF HTML,OpenDocument, PDF,EPUB, XPS转换
            doc.save(os, SaveFormat.PDF);
            long now = System.currentTimeMillis();
            System.out.println("convert OK! " + ((now - old) / 1000.0) + "秒");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void doc2pdf(InputStream inPath, OutputStream bos) throws Exception {
        if (!getLicense()) {
            throw new Exception("com.aspose.words lic ERROR!");
        }
        try {
            long old = System.currentTimeMillis();
            LoadOptions loadOptions = new LoadOptions();
            FontSettings f = new FontSettings();
            //f.setFontsFolder("/application/server/fonts", false);

            f.setFontsFolder("/application/server/fonts", false);
            loadOptions.setFontSettings(f);
            Document doc = new Document(inPath,loadOptions);
            // 支持RTF HTML,OpenDocument, PDF,EPUB, XPS转换
            doc.save(bos, SaveFormat.PDF);
            long now = System.currentTimeMillis();
            System.out.println("convert OK! " + ((now - old) / 1000.0) + "秒");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static boolean getCellLicense() {
        boolean result = false;
        InputStream is = Doc2Pdf.class.getClassLoader().getResourceAsStream("license.xml");
        com.aspose.cells.License aposeLic = new com.aspose.cells.License();
        try {
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static void excel2pdf( String inPath,  String outPath) throws Exception {
        // 验证License
        if (!getCellLicense()) {
            return;
        }
        try {
            File file = new File(outPath);
            FileOutputStream os = new FileOutputStream(file);
            Workbook doc = new Workbook(inPath);
            doc.save(os, com.aspose.cells.SaveFormat.PDF);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void excel2pdf( InputStream inPath, OutputStream bos) throws Exception {
        // 验证License
        if (!getCellLicense()) {
            return;
        }
        try {
            Workbook doc = new Workbook(inPath);
            doc.save(bos, com.aspose.cells.SaveFormat.PDF);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


//    private static class SingletonHandler {
//        private static Doc2Pdf singleton = new Doc2Pdf();
//    }
//
//    public static Doc2Pdf getInstance(){
//        return SingletonHandler.singleton;
//    }


}
